package com.gnerv.sylvanas.business.manage.operation;

import com.gnerv.sylvanas.framework.core.BaseCurrentAccount;
import com.gnerv.sylvanas.business.manage.model.bo.ApiBO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiDTO;
import com.gnerv.sylvanas.business.manage.model.entity.Api;
import com.gnerv.sylvanas.business.manage.storage.ApiStorageService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

import static com.gnerv.sylvanas.framework.common.tools.PageTools.limitSize;
import static com.gnerv.sylvanas.framework.common.tools.PageTools.limitStart;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class ApiOperationService {

    private final ApiStorageService apiStorageService;
    private final BaseCurrentAccount baseCurrentAccount;

    
    public boolean create(ApiBO apiBO) {
        Api api = apiBO.toApi();
        apiStorageService.insert(api);
        return true;
    }

    
    public boolean deleteByUkId(String ukId) {
        apiStorageService.deleteByUkId(ukId);
        return true;
    }

    
    public boolean updateByUkId(ApiBO apiBO) {
        Api api = apiBO.toApi();
        apiStorageService.updateByUkId(api);
        return true;
    }

    
    public ApiDTO selectByUkId(String ukId) {
        return apiStorageService.selectByUkId(ukId);
    }

    
    public List<ApiDTO> selectListByName(String name, int page, int size) {
        return apiStorageService.selectListByName(name, limitStart(page, size), limitSize(size));
    }

    
    public List<ApiDTO> selectListByRoleUkId(String roleUkId) {
        return apiStorageService.selectListByRoleUkId(roleUkId);
    }

    
    public Set<String> selectListAuthorityByAccountUkId(String accountUkId) {
        return apiStorageService.selectListAuthorityByAccountUkId(accountUkId);
    }

    
    public String selectAuthorityByUriAndType(String url, String type) {
        return apiStorageService.selectAuthorityByUriAndType(url, type);
    }

    
    public int selectCountByName(String name) {
        return apiStorageService.selectCountByName(name);
    }

    
    public boolean isExist(String uri, String type) {
        return apiStorageService.selectCountByUriAndType(uri, type) > 0;
    }

    
    public boolean bindRole(String apiUkId, Set<String> roleUkIdList) {
        apiStorageService.bindRole(apiUkId, roleUkIdList, baseCurrentAccount.ukId());
        return true;
    }

    
    public boolean unbindRole(String apiUkId) {
        apiStorageService.unbindRole(apiUkId);
        return true;
    }

    
    public List<ApiDTO> selectListByGroupUkId(String groupUkId, int page, int size) {
        return apiStorageService.selectListByGroupUkId(groupUkId, limitStart(page, size), limitSize(size));
    }

    
    public List<ApiDTO> selectListByAccountUkId(String accountUkId) {
        return apiStorageService.selectListByAccountUkId(accountUkId);
    }
}
